<template>
  <div class="app-container">
    <el-scrollbar :wrap-class="{wrapClass: true}" class="form-scrollbar">

        <el-card class="box-card" shadow="never">
          <div slot="header" class="clearfix">
            <span>基础信息</span>
          </div>

          <el-form ref="form" class="form-company" :rules="rules" :model="info" label-width="110px">
            <el-form-item label="公司名称" prop="name">
              <el-input v-model="info.name" @input="checkTrim('name')"/>
            </el-form-item>
            <el-form-item label="账户有效期">
              <el-input v-model="info.validity" disabled/>
            </el-form-item>
            <el-form-item label="员工授权数">
              <el-input :value="info.isOpenUser === 1 ? '不限制':info.openUserNum+' 人'" disabled/>
            </el-form-item>
            <el-form-item label="可用积分">
              <template v-if="info.isUnlimitedPoints === 1">
                <el-input value="不限制" disabled></el-input>
              </template>
              <template v-else>
                <el-input :value="info.numberOfPoints + ' 分'" disabled></el-input>
              </template>
            </el-form-item>
            <el-form-item label="线索唯一性">
              <el-radio-group v-model="info.isRepetitionAllowed">
                <el-radio :label="0">关闭</el-radio>
                <el-radio :label="1">开启</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="公海时间">
                <el-input-number v-model="info.autoWatersDay" :max="999999" :min="1"></el-input-number>
            </el-form-item>
          </el-form>
        </el-card>
        <span style="padding-bottom: 30px">
          <el-button type="primary" :loading="infoLoading" @click="subInfo">立即提交</el-button>
        </span>

    </el-scrollbar>
  </div>
</template>
<style scoped>
.app-container {
  padding-top: 20px;
  height: calc(100vh - 65px);
}

.form-scrollbar {
  height: calc(100vh - 75px);
}

.form-company {
  min-width: 720px;
}

.el-dialog-div {
  height: 290px;
}

.el-dialog-div /deep/ .el-alert {
  margin-bottom: 20px;
}

.box-card {
  margin-bottom: 20px;
}

.box-card /deep/ .el-card__header {
  padding: 10px 20px;
}

.box-card /deep/ .el-card__body {
  padding: 20px 10px;
}
</style>

<script>
import { postAction } from '@/api/common'
import common from '@/utils/common'

export default {
  name: 'index',
  data() {
    return {
      info: {
        name: '',
        validity: '',
        isOpenUser: 1,
        openUserNum: 0,
        isRepetitionAllowed: 0,
        autoWatersDay: 15,
        isUnlimitedPoints: 1,
        numberOfPoints: 0
      },
      infoLoading: true,
      rules: {
        // element form表单元素验证
        name: [
          {
            required: true,
            message: '请输入公司名',
            trigger: 'change'
          }
        ]
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    async init() {
      this.infoLoading = true
      const { data } = await postAction({}, 'UserCompany/GetSysCompanyConfig')
      this.info = data
      this.infoLoading = false
    },
    subInfo() {
      this.infoLoading = true
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          this.infoLoading = true
          await postAction(this.info, 'UserCompany/SubSysCompanyConfig')

          this.infoLoading = false
          this.$notify({
            title: '成功',
            message: '操作成功',
            type: 'success',
            duration: 2000
          })
        }
      })
    },
    checkTrim(obj) {
      this.info[obj] = common.checkAllTrim(this.info[obj])
    }
  }
}
</script>
